<?php

/**
 * Converts a sql query to an array for easier usage.
 */

function queryToArray($sql) {
	$array = array();

	while($row = mysqli_fetch_object($sql)) {
		$array[] = $row;
	}
	return $array;
}

 
/**
 * Creates query and prints error message if it failed.
 * @param $sqlQuery
 * @return unknown_type
 */

function createQuery($sqlQuery) {
	$sql = mysqli_query(connectToDatabase(), $sqlQuery);
	if (!$sql) {
		echo "SQL: ".$sqlQuery."<br/>";
		die('Invalid query: ' . mysqli_error());
	}
	return $sql;
}

function errorOut($code, $message) {
	\Slim\Slim::getInstance()->halt(500, "{ 'code' : '$code', 'message' : '$message' }");
}


function json_decode_or_die($data) {
	$result = json_decode($data);

	switch(json_last_error()) {
		case JSON_ERROR_DEPTH:
			errorOut('JSON_ERROR', 'Stack overflow error!');
			break;
		case JSON_ERROR_CTRL_CHAR:
			errorOut('JSON_ERROR', 'Invalid char found!');
			break;
		case JSON_ERROR_SYNTAX:
			errorOut('JSON_ERROR', 'Syntax error!');
			break;
		default:
			return $result;
	}
}



/*
 *  CONCRETE IMPLEMENTATION FOR "WAS BIN ICH"
*/

function createPictureSelect($language) {
	return "SELECT id, section, bigPicture, correctAnswers_$language as correctAnswers, description_$language as description
			FROM ".TABLE_PICTURE;
}

function createManagerPictureSelect() {
	return "SELECT id, correctAnswers_de, description_de, correctAnswers_en, description_en
			FROM ".TABLE_PICTURE;
}

function createFullManagerPictureSelect() {
	return "SELECT id, section, bigPicture, correctAnswers_de, description_de, correctAnswers_en, description_en
			FROM ".TABLE_PICTURE;
}
?>